package com.qb.devserv;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.xuexiang.xui.XUI;

import utils.DevActionUtil;
import utils.MqttUtil;
import utils.MyUtil;

public class DialogActivity extends AppCompatActivity {

    Context ctx;
    MqttUtil mu = new MqttUtil();
    TextView tvTitle,tvTip,tvMsg;
    Button btnReset,btnOk,btnCancel;
    String flag,p1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        XUI.initTheme(this);
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_dialog);
        Window window = this.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.CENTER;
        lp.width = 920;
        lp.height = 290;
        window.setAttributes(lp);
        window.setDimAmount(0.1f);  //背景透明
        ctx = DialogActivity.this;

        Intent exStr = this.getIntent();
        tvTitle = findViewById(R.id.tv_dialog_title);
        tvTip = findViewById(R.id.tv_dialog_tip);
        tvMsg = findViewById(R.id.tv_dialog_msg);
        flag = exStr.getStringExtra("flag");
        p1 = exStr.getStringExtra("parm1");
        tvTitle.setText(exStr.getStringExtra("title"));
        tvMsg.setText(exStr.getStringExtra("msg"));
        String mqttAction = "powerdown,reset,update,reUpdate,factory";
        btnReset = findViewById(R.id.btn_dialog_reset);
        btnOk = findViewById(R.id.btn_dialog_ok);
        btnCancel = findViewById(R.id.btn_dialog_cancel);

        if (flag.equals("error")) {
            btnOk.setVisibility(View.GONE);
            btnCancel.setVisibility(View.GONE);
            tvMsg.setTextColor(getResources().getColor(R.color.red_start));
            Drawable errIcon = getResources().getDrawable(R.drawable.icon_baseline_info_error_32);
            errIcon.setBounds(0, 0, errIcon.getMinimumWidth(),errIcon.getMinimumHeight());
            tvTitle.setCompoundDrawables(errIcon,null,null,null);
        } else if (mqttAction.contains(flag)) {
            btnReset.setVisibility(View.GONE);
        } else {
            btnReset.setVisibility(View.GONE);
            btnOk.setVisibility(View.GONE);
        }
        btnReset.setOnClickListener(view -> {
            //startActivities(new Intent[]{new Intent().setClass(this, MainActivity.class)});
            finish();
        });
        btnOk.setOnClickListener(view -> {
            DevActionUtil.okAppDialog(ctx,flag,p1);
            finish();
        });
        btnCancel.setOnClickListener(view -> {
            cancel();
        });
    }

    @Override
    public void onBackPressed() {
        cancel();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public void cancel() {
        //DevActionUtil.cancelAppDialog(ctx,flag,p1);
        finish();
    }
}